package com.dify.tool.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dify.tool.api.dto.TechnicalGenerateRecordDTO;
import com.dify.tool.api.vo.TechnicalGenerateRecordVO;
import com.dify.tool.dal.entity.TechnicalGenerateRecord;
import com.dify.tool.dal.mapper.TechnicalGenerateRecordMapper;
import com.dify.tool.service.ITechnicalGenerateRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 代码生成业务表 服务实现类
 * </p>
 *
 * @author shl
 * @since 2025-06-03
 */
@Slf4j
@Service
public class TechnicalGenerateRecordServiceImpl extends ServiceImpl<TechnicalGenerateRecordMapper, TechnicalGenerateRecord> implements ITechnicalGenerateRecordService {
    @Override
    public IPage<TechnicalGenerateRecordVO> pageList(Page page, TechnicalGenerateRecordDTO recordDTO) {
        IPage<TechnicalGenerateRecordVO> res = baseMapper.page(page, recordDTO);
        //获取点赞状态
        List<String> messageIds = res.getRecords().stream().map(TechnicalGenerateRecordVO::getMessageId).toList();
        List<TechnicalGenerateRecordVO> ratingByMessageIds = baseMapper.getRatingByMessageIds(messageIds);
        res.getRecords().forEach(in -> {
            if (ObjectUtil.isNotEmpty(ratingByMessageIds)) {
                List<TechnicalGenerateRecordVO> currentRating = ratingByMessageIds.stream().filter(r -> ObjectUtil.equal(in.getMessageId(), r.getMessageId())).toList();
                in.setRating(ObjectUtil.isNotEmpty(currentRating) ? currentRating.get(0).getRating() : null);
            }
        });
        return res;
    }
}
